
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Friend implements Serializable{

    private String name;
    private int iD;
    private Set<String> eMail;
    private int ceasarKey;
    private int subKey;
    private String DESKey;
    private String DESKey2;

    public Friend(int friendCount) {
        name = "";
        eMail = new HashSet<>();
        iD = friendCount;
        ceasarKey = 0;
        subKey = 0;
        DESKey = "";
        DESKey2 = "";
    }

    public Friend(String fName, String fEMail, int friendCount) {
        name = fName;
        eMail = new HashSet<String>();
        eMail.add(fEMail);
        iD = friendCount;
        ceasarKey = 0;
        subKey = 0;
        DESKey = "";
        DESKey2 = "";
    }

    public int getID() {
        return iD;
    }

    //only sets name if fName is valid
    public boolean setName(String fName) {
        if (validateName(fName)) {
            name = fName;
            return true;
        }
        return false;
    }

    //a valid name must be only letters and spaces,
    //if any characters in the string are NOT space or a letter,
    //returns false.
    public boolean validateName(String name) {
        for (int i = 0; i < name.length(); i++) {
            if (name.charAt(i) != ' ' && !(name.charAt(i) >= 65
                    && name.charAt(i) <= 90) && !(name.charAt(i) >= 97
                    && name.charAt(i) <= 122)) {
                return false;
            }
        }
        return true;
    }

    public String getName() {
        return name;
    }

    public boolean addEMail(String fEMail) {
        if (validateEMail(fEMail)) {
            eMail.add(fEMail);
            return true;
        }
        return false;
    }

    //an email must be at least length 5 a@a.a to potentially be valid
    //a valid email will contain the @ symbol and a period to the right of
    //the @ symbol
    public boolean validateEMail(String fEMail) {
        if (fEMail.length() < 5) {
            return false;
        }
        if (fEMail.contains("@") && fEMail.substring(fEMail.indexOf("@"))
                .contains(".")) {
            return true;
        }
        return false;
    }

    //returns false and does not delete the email if it is the last email
    //or if the email is not within the set
    public boolean deleteEMail(String fEMail) {
        if (eMail.size() <= 1 || !eMail.contains(fEMail)) {
            return false;
        }
        eMail.remove(fEMail);
        return true;
    }

    public Set<String> getEMail() {
        return eMail;
    }

    //valid ceasar key cannot be a multiple of 26, will not change 
    //the ciphertext
    public boolean setCeasarKey(int key) {
        if ((key % 26) != 0) {
            ceasarKey = key;
            return true;
        }
        return false;
    }

    // is there validation?
    public boolean setSubKey(int key) {
        subKey = key;
        return true;
    }

    public int getCeasarKey() {
        return ceasarKey;
    }

    public int getSubKey() {
        return subKey;
    }

    public String getDESKey() {
        return DESKey;
    }

    public boolean setDESKey(String DESKey) {
        if (DESKey.equals("") || DESKey == null) {
            return false;
        }
        this.DESKey = DESKey;
        return true;
    }

    public String getDESKey2() {
        return DESKey2;
    }

    public boolean setDESKey2(String DESKey2) {
        if (DESKey.equals("") || DESKey == null) {
            return false;
        }
        this.DESKey2 = DESKey2;
        return true;
    }

    
    @Override
    public String toString() {
        String output = "Name: " + name + "\nID: " + iD + "\nAddresses: \n";
        Iterator iter = eMail.iterator();
        while (iter.hasNext()) {
            output += iter.next() + "\n";
        }
        output += "Simplemonoalphabetic encryption key: " + ((getCeasarKey() 
                == 0) ? "Empty" : getCeasarKey());
        output += "\nPolyalphabetic encryption key: " + ((getSubKey() == 0) ? 
                "Empty" : getSubKey());
        output += "\nDES key: " + ((getDESKey().equals("")) ? "Empty" : 
                getDESKey());
        output += "\n3DES key: " + ((getDESKey2().equals("")) ? "Empty" : 
                getDESKey2());
        return output;
    }
}